package cn.kgc.bdqn_studentinfomanager.service.CommService.impl;

import cn.kgc.bdqn_studentinfomanager.dao.comm.CommMapper;
import cn.kgc.bdqn_studentinfomanager.pojo.common.ServerResponse;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.*;
import cn.kgc.bdqn_studentinfomanager.service.CommService.CommService;
import cn.kgc.bdqn_studentinfomanager.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * by Shanshui CC on 2020/12/12 9:39
 */
@Service("/commService")
public class CommServiceImpl implements CommService {

    @Resource
    private CommMapper commMapper;

    /**
     * 进入添加专员页面时加载地区下拉框
     * @param token
     * @return
     */
    @Override
    public ServerResponse loadingCommAddress(String token) {

        if(StringUtils.isBlank(token)){
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        //从token中拿到用户id
        String id = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));
        List<ProvinceAndCityVo> provinceVoList = commMapper.getProvinceById(id);

        return ServerResponse.createBySuccess(provinceVoList);
    }

    /**
     * 渠道主管添加渠道专员
     * @param addUserVo
     * @param token
     * @return
     */
    @Override
    public ServerResponse addcommissioner(AddUserVo addUserVo, String token) {

        if(addUserVo == null || StringUtils.isBlank(token)){
            return ServerResponse.createByErrorMessage("信息不能为空");
        }
        //从token中拿到用户id
        String parentId = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));
        addUserVo.setParentId(parentId);
        //校验密码
        addUserVo.setUserPassword(MD5Util.MD5EncodeUtf8(addUserVo.getUserPassword()));

        Integer integer = commMapper.addCommissioner(addUserVo);
        if(integer >= 1){
            return ServerResponse.createBySuccessMessage("添加成功");
        }

        return ServerResponse.createByErrorMessage("添加失败");
    }


    /**
     *  初始化咨询师
     * @return
     */
    @Override
    public ServerResponse loadingConInfo() {

        List<ShowRoleVo> showRoleVoList = commMapper.queryCon();

        if (showRoleVoList == null){
            return ServerResponse.createByErrorMessage("初始化咨询师列表失败");
        }

        return ServerResponse.createBySuccess(showRoleVoList);

    }


    /**
     * 分配咨询师
     * @param studentIdList
     * @param conId
     * @return
     */
    @Override
    public ServerResponse distributionStudent(List<String> studentIdList, String conId) {

        if(studentIdList == null || StringUtils.isBlank(conId)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        //分配学生前判断该学生是否被分配
        Integer selectStudentStatus = commMapper.selectStudentStatus(studentIdList);
        if(selectStudentStatus >= 1){
            return ServerResponse.createByErrorMessage("选择的学生中有被分配");
        }

        //分配学生给咨询师
        Integer integer = commMapper.updateStudentStatus(studentIdList,conId);

        if(integer > 0){
            return ServerResponse.createBySuccessMessage("分配成功");
        }

        return ServerResponse.createByErrorMessage("分配失败");

    }
}
